//
//  CoolSlidingMenuView.m
//  333333
//
//  Created by 刘应平 on 2017/8/31.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "CoolSlidingMenuView.h"
#import "TopImageViewAndBottomLabelView.h"
#import "TopImageAndLabelModel.h"
@interface CoolSlidingMenuView ()<UIScrollViewDelegate>
/**  数据数组  */
@property (strong, nonatomic) NSArray  *data;
/**  显示几行  */
@property (assign, nonatomic) NSInteger  section;
/**  显示几列  */
@property (assign, nonatomic) NSInteger  row;
/**  显示几页  */
@property (assign, nonatomic) NSInteger  page;

/**  底层的滚动视图  */
@property (strong, nonatomic) UIScrollView  *backScrollView;
/**  pageController  */
@property (strong, nonatomic) UIPageControl  *pageControl;


@end
@implementation CoolSlidingMenuView

- (instancetype)initWithFrame:(CGRect)frame section:(NSInteger)section row:(NSInteger)row data:(NSArray *)data
{
    self = [super initWithFrame:frame];
    if (self) {
        self.data = data;
        self.section = section;
        self.row = row;
        self.page = self.data.count / (row * section);
        NSInteger yu = self.data.count % (row * section);
        if (yu > 0) {
            self.page += 1;
        }
        [self layoutUI];
        if (self.page == 1) {
            self.pageControl.hidden = YES;
        }
    }
    return self;
}

- (void)layoutUI{
    [self addSubview:self.backScrollView];
    [self addSubview:self.pageControl];
    
    CGFloat with = [UIScreen mainScreen].bounds.size.width / self.row;
    
    for (int i = 0; i < self.data.count; i ++) {
        TopImageAndLabelModel *model = self.data[i];
        /**  高度  */
        NSInteger zheng = i / self.row ;
        NSInteger yu = i % self.row;
        zheng = zheng % self.section;
        /**  宽度  */
        NSInteger count = i / (self.row * self.section);
        
        TopImageViewAndBottomLabelView *imageLabel = [[TopImageViewAndBottomLabelView alloc] initWithFrame:CGRectMake(with * yu + count * [UIScreen mainScreen].bounds.size.width, zheng * 80 * [UIScreen zj_screenHScale], with, 80 * [UIScreen zj_screenHScale]) count:self.row];
        imageLabel.topImageView.image = [UIImage imageNamed:model.imageString];
        imageLabel.bottomLabel.text = model.titleString;
        imageLabel.tag = i;
        imageLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)];
        [imageLabel addGestureRecognizer:tap];
        
        [self.backScrollView addSubview:imageLabel];
    }

}

- (void)tapView:(UITapGestureRecognizer *)tap{
    if (self.delegate ) {
        [self.delegate CoolSlidingMenuViewWithView:self clickIndex:tap.view.tag];
    }

}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.pageControl.currentPage = scrollView.contentOffset.x / self.bounds.size.width;

}


#pragma mark - getter
- (UIScrollView *)backScrollView{
    if (_backScrollView == nil) {
        _backScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _backScrollView.showsVerticalScrollIndicator = NO;
        _backScrollView.showsHorizontalScrollIndicator = NO;
        _backScrollView.pagingEnabled = YES;
        _backScrollView.bounces = YES;
        _backScrollView.backgroundColor = [UIColor whiteColor];
        _backScrollView.delegate = self;
        _backScrollView.contentSize = CGSizeMake(self.page * self.bounds.size.width, self.bounds.size.height);
        
    }
    return _backScrollView;
}

- (UIPageControl *)pageControl{
    if (_pageControl == nil) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 10 * [UIScreen zj_screenHScale], self.bounds.size.width, 15 * [UIScreen zj_screenHScale])];
        _pageControl.numberOfPages = self.page;
        _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        _pageControl.pageIndicatorTintColor = [UIColor blueColor];
        
    }
    return _pageControl;
}

#pragma mark - setter
- (void)setDefeatColor:(UIColor *)defeatColor{
    _defeatColor = defeatColor;
    self.pageControl.pageIndicatorTintColor = defeatColor;
}

- (void)setCurrentColor:(UIColor *)currentColor{
    _currentColor = currentColor;
    self.pageControl.currentPageIndicatorTintColor = currentColor;
}
- (void)setIsBounce:(BOOL)isBounce{
    _isBounce = isBounce;
    self.backScrollView.bounces = isBounce;

}
- (void)setIsPagingEnabled:(BOOL)isPagingEnabled{
    _isPagingEnabled = isPagingEnabled;
    self.backScrollView.pagingEnabled = isPagingEnabled;
}

- (void)setIsShowPageCtr:(BOOL)isShowPageCtr{
    _isShowPageCtr = isShowPageCtr;
    self.pageControl.hidden = isShowPageCtr;
}

@end

